package user

import (
	"api/internal/svc"
	"api/internal/types"
	"api/pb/user"
	"api/pkg/metadata"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserInfoLogic {
	return &GetUserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserInfoLogic) GetUserInfo() (resp *types.User, err error) {
	userId, err := metadata.GetUserIDFromCtx(l.ctx)
	if err != nil {
		return nil, err
	}
	u, err := l.svcCtx.UserRpc.GetUserByID(l.ctx, &user.IDRequest{Id: userId})
	if err != nil {
		return nil, err
	}
	return &types.User{Id: u.Id, Name: u.Name}, nil
}
